package com.qishon.pc.domain.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
/**
 * Created by yuquan.hu on 2017/1/5.
 */
@Slf4j
@Component


public class HttpRequestUtils {
    private static final long TIMEOUT = 60000;

    private static final String CONTENT_CHARSET = "UTF-8";
    /**
     * @Description:post 璇锋眰
     * @param url
     * @param param
     * @return
     * @throws IOException
     * @author zhizhao.wang
     * @date 2015骞�鏈�4鏃�涓嬪崍17:46:53
     */
    public static HttpResult sendByPost(String url, Map<String, String> param) throws IOException {
        HttpClient httpClient = null;
        PostMethod postMethod = null;
        BufferedReader bs = null;
        try {
            httpClient = new HttpClient(new HttpClientParams(), new SimpleHttpConnectionManager(true));
            httpClient.getParams().setContentCharset(CONTENT_CHARSET);
            postMethod = new PostMethod(url);
            if (param != null && !param.isEmpty()) {
                final Iterator<String> iter = param.keySet().iterator();
                while (iter.hasNext()) {
                    final String k = iter.next();
                    String v = param.get(k);
                    if (v == null) {
                        v = "";
                    }
                    postMethod.addParameter(new NameValuePair(k, v));
                }
            }
            httpClient.getHttpConnectionManager().getParams().setConnectionTimeout((int) TIMEOUT); // 璁剧疆杩炴帴瓒呮椂
            postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, (int) TIMEOUT); // 璁剧疆璇锋眰瓒呮椂
            final int resultCode = httpClient.executeMethod(postMethod);

            final StringBuffer resultBuffer = new StringBuffer();
            bs = new BufferedReader(new InputStreamReader(postMethod.getResponseBodyAsStream(), CONTENT_CHARSET));
            String line = null;
            while ((line = bs.readLine()) != null) {
                resultBuffer.append(line);
            }
            return new HttpResult(resultCode, resultBuffer.toString());
        } catch (UnsupportedEncodingException e) {
            throw e;
        } catch (IOException e) {
            throw e;
        } finally {
            if (null != bs) {
                try {
                    bs.close();
                } catch (IOException e) {
                    throw e;
                }
            }
            if (postMethod != null) {
                postMethod.releaseConnection();
                if (null != httpClient) {
                    ((SimpleHttpConnectionManager) httpClient.getHttpConnectionManager()).shutdown();
                }
            }
        }
    }
}
